package com.estate.struts.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.estate.business.UserInfoService;
import com.estate.utils.EncodeUtil;
import com.estate.utils.ProjectConstants;
import com.estate.utils.RoleUtil;
import com.estate.utils.SexUtil;
import com.estate.vo.Vo;
import com.estate.vo.impl.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class UserInfoAction extends EstateSupport {

	private String id;
	private String loginName;
	private String password;
	private String name;
	private String role;
	private String sex;
	private String birthday;
	private String idCard;
	private String phone;
	private String education;
	private String address;
	private String workDate;
	private String serialNum;
	
	//添加字段
	private String profession;
	private String roleQuali;
	
	private String newPassword;
	
	private String method;
	
	private String multiType;
	
	private String multiDelete;
	private String delStr;
	
	private UserInfoService userInfoService;
	
	@Override
	public String execute() throws Exception {
		System.out.println("method ================= " + this.getMethod());

		String page = "";
		if("add".equals(this.getMethod())){
			UserInfoVo userInfo = this.getAddUserInfo();
			if(userInfo != null){
				boolean result = this.getUserInfoService().addUserInfo(userInfo);
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
			}else{
				page="fail";
			}
		}else if("list".equals(this.getMethod())){
			List<Vo> userInfoList = this.getUserInfoService().getAllUserInfo();
			this.getRequest().setAttribute("userInfoList", userInfoList);
//			System.out.println(this.getMultiType());
			if(this.getMultiType() != null && this.getMultiType().equals("true")){
				this.getRequest().setAttribute("multiType", new Boolean(true));
//				System.out.println("set multiType "+ this.getRequest().getAttribute("multiType"));
			}else{
				this.getRequest().setAttribute("multiType", new Boolean(false));
//				System.out.println("set singleType "+ this.getRequest().getAttribute("multiType"));
			}
			page="allUserInfo";
		}else if("detail".equals(this.getMethod())){
			Vo userInfo = userInfoService.getUserInfo(Integer.parseInt(this.getId()));
			this.getRequest().setAttribute("userInfo", userInfo);
			page = "detail";
		}else if("modify".equals(this.getMethod())){
			Vo userInfo = null;
			if(this.getId() == null || "".equals(this.getId())){
				userInfo = userInfoService.getUserInfo(Integer.parseInt(this.getId()));
			}else{
				userInfo = userInfoService.getUserInfo(this.getSessionUser().getId());
			}
			
			this.getRequest().setAttribute("userInfo", userInfo);
			page = "update";
		}else if("update".equals(this.getMethod())){
			UserInfoVo userInfo = this.getUpdateUserInfo();
			if(userInfo == null){
				page = "fail";
			}else{
				userInfo = this.getUserInfoService().updateUserInfo(userInfo);
				System.out.println(userInfo.getWorkDate());
				if(userInfo != null){
					page=this.SUCCESS;
					this.setSessionUser(userInfo);
				}else{
					page="fail";
				}
			}
		}else if("delete".equals(this.getMethod())){
			if("true".equals(this.multiDelete)){
				System.out.println(this.getDelStr().split(",").length);
				boolean result = this.getUserInfoService().deleteMultiUser(this.getDelStr());
				if(result){
					page=this.SUCCESS;
				}else{
					page="fail";
				}
				
			}else{
				System.out.println("single delete");
				if(this.getId() == null || "".equals(this.getId())){
					page="faild";
				}else{
					UserInfoVo userInfo = new UserInfoVo();
					userInfo.setId(Integer.parseInt(this.getId()));
					boolean result = userInfoService.deleteUserInfo(userInfo);
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}
			}
		}else if("changePassword".equals(this.getMethod())){
			UserInfoVo sessionUser = this.getSessionUser();
			if(this.getNewPassword() == null || "".equals(this.getNewPassword()) || this.getPassword() == null){
				page="fail";
			}else{
				if(sessionUser.getPassword().equals(EncodeUtil.getMD5Code(this.getPassword()))){
					boolean result = this.getUserInfoService().updatePassword(sessionUser, this.getNewPassword());
					if(result){
						page=this.SUCCESS;
					}else{
						page="fail";
					}
				}else{
					page="fail";
				}
			}
		}else{
			page="fail";
		}
		return page;
	}
	private UserInfoVo getUpdateUserInfo(){
		UserInfoVo userInfo = new UserInfoVo();
		try {
			if(this.getId() == null || "".equals(this.getId())){
				return null;
			}
			userInfo.setId(Integer.parseInt(this.getId()));
			userInfo.setAddress(this.getAddress());
			userInfo.setEducation(this.getEducation());
			userInfo.setIdCard(this.getIdCard());
			userInfo.setPhone(this.getPhone());
//			userInfo.setRegistDate(this.getSessionUser().getRegistDate());
			userInfo.setSerialNum(this.getSerialNum());
			userInfo.setSex(SexUtil.getSexString(Integer.parseInt(this.getSex())));
			if(this.getBirthday() != null && !"".equals(this.getBirthday())){
				userInfo.setBirthday(ProjectConstants.SIMPLE_DATE.parse(this.getBirthday()));
			}
			if(this.getWorkDate() != null && !"".equals(this.getWorkDate())){
				userInfo.setWorkDate(ProjectConstants.SIMPLE_DATE.parse(this.getWorkDate()));
			}
			userInfo.setProfession(this.getProfession());
			userInfo.setRoleQuali(this.getRoleQuali());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return userInfo;
	}
	private UserInfoVo getAddUserInfo(){
		UserInfoVo userInfo = new UserInfoVo();
		
		try {
			if(this.getLoginName() == null || this.getName() == null){
				return null;
			}
			if(this.getBirthday() != null && !"".equals(this.getBirthday())){
				userInfo.setBirthday(ProjectConstants.SIMPLE_DATE.parse(this.getBirthday()));
			}
			if(this.getWorkDate() != null && !"".equals(this.getWorkDate())){
				userInfo.setWorkDate(ProjectConstants.SIMPLE_DATE.parse(this.getWorkDate()));
			}
			userInfo.setAddress(this.getAddress());
			userInfo.setEducation(this.getEducation());
			userInfo.setIdCard(this.getIdCard());
			userInfo.setLoginName(this.getLoginName());
			userInfo.setName(this.getName());
			userInfo.setPassword(EncodeUtil.getMD5Code("123456"));
			userInfo.setPhone(this.getPhone());
			userInfo.setRegistDate(new Date());
			userInfo.setRole(RoleUtil.getRoleString(Integer.parseInt(this.getRole())));
			userInfo.setSerialNum(this.getSerialNum());
			userInfo.setSex(SexUtil.getSexString(Integer.parseInt(this.getSex())));
			userInfo.setProfession(this.getProfession());
			userInfo.setRoleQuali(this.getRoleQuali());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return userInfo;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getIdCard() {
		return idCard;
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEducation() {
		return education;
	}

	public void setEducation(String education) {
		this.education = education;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getWorkDate() {
		return workDate;
	}

	public void setWorkDate(String workDate) {
		this.workDate = workDate;
	}

	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}

	public String getMultiType() {
		return multiType;
	}

	public void setMultiType(String multiType) {
		this.multiType = multiType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getMultiDelete() {
		return multiDelete;
	}
	public void setMultiDelete(String multiDelete) {
		this.multiDelete = multiDelete;
	}
	public String getDelStr() {
		return delStr;
	}
	public void setDelStr(String delStr) {
		this.delStr = delStr;
	}
	public String getProfession() {
		return profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}
	public String getRoleQuali() {
		return roleQuali;
	}
	public void setRoleQuali(String roleQuali) {
		this.roleQuali = roleQuali;
	}
	
}
